package la.iok.hzsvn.lewin.movie.operation.cfg;

import la.iok.hzsvn.share.utils.FileUtils;
import la.iok.hzsvn.share.utils.PathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.IOException;

@ConfigurationProperties(prefix = "app.payment.ali")
public class AliPayProperties {
    private static final Logger logger = LoggerFactory.getLogger(AliPayProperties.class);
    /**
     * 证书文件存储路径
     */
    private String certPath = "/app/cert/ali";

    /**
     * AES密钥文件名称
     */
    private String aesFileName = "alipay aes.txt";
    /**
     * 支付宝公钥证书文件名
     */
    private String alipayCertPubKeyFileName = "alipayCertPublicKey_RSA2.crt";
    /**
     * 支付宝根证书文件名
     */
    private String alipayRootCertFileName = "alipayRootCert.crt";
    /**
     * 应用公钥证书文件名
     */
    private String appCertPubKeyFileName = "appCertPublicKey_2021003179647139.crt";
    /**
     * 应用公钥文件名
     */
    private String appPubFileName = "应用公钥RSA2048.txt";
    /**
     * 应用私钥文件名
     */
    private String appPriFileName = "应用私钥RSA2048-敏感数据，请妥善保管.txt";

    public String getCertPath() {
        return certPath;
    }

    public void setCertPath(String certPath) {
        this.certPath = certPath;
    }

    public String getAesFileName() {
        return aesFileName;
    }

    public void setAesFileName(String aesFileName) {
        this.aesFileName = aesFileName;
    }

    public String getAlipayCertPubKeyFileName() {
        return alipayCertPubKeyFileName;
    }

    public void setAlipayCertPubKeyFileName(String alipayCertPubKeyFileName) {
        this.alipayCertPubKeyFileName = alipayCertPubKeyFileName;
    }

    public String getAlipayRootCertFileName() {
        return alipayRootCertFileName;
    }

    public void setAlipayRootCertFileName(String alipayRootCertFileName) {
        this.alipayRootCertFileName = alipayRootCertFileName;
    }

    public String getAppCertPubKeyFileName() {
        return appCertPubKeyFileName;
    }

    public void setAppCertPubKeyFileName(String appCertPubKeyFileName) {
        this.appCertPubKeyFileName = appCertPubKeyFileName;
    }

    public String getAppPubFileName() {
        return appPubFileName;
    }

    public void setAppPubFileName(String appPubFileName) {
        this.appPubFileName = appPubFileName;
    }

    public String getAppPriFileName() {
        return appPriFileName;
    }

    public void setAppPriFileName(String appPriFileName) {
        this.appPriFileName = appPriFileName;
    }

    public String aesKey(){
        try {
            byte[] data = FileUtils.readFile(PathUtils.join(certPath,aesFileName));
            if(data == null || data.length == 0){
                return "";
            }
            return new String(data);
        } catch (IOException e) {
            logger.error("读取AES密钥失败",e);
            return null;
        }
    }

    public String merchantPrivateKey() {
        try {
            byte[] data = FileUtils.readFile(PathUtils.join(certPath,appPriFileName));
            if(data == null || data.length == 0){
                return "";
            }
            return new String(data);
        } catch (IOException e) {
            logger.error("读取应用私钥失败",e);
            return null;
        }
    }
    public String merchantCertPath(){
        return PathUtils.join(certPath,appCertPubKeyFileName);
    }

    public String alipayCertPath(){
        return PathUtils.join(certPath, alipayCertPubKeyFileName);
    }

    public String alipayRootCertPath(){
        return PathUtils.join(certPath,alipayRootCertFileName);
    }
}
